package top.lushi78778.iot.mqtt.web.api.v1;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.onsmqtt20200420.models.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.lushi78778.iot.mqtt.configurer.MqttConfigurer;
import top.lushi78778.iot.mqtt.core.MqttClient;
import top.lushi78778.iot.mqtt.core.Result;
import top.lushi78778.iot.mqtt.core.ResultGenerator;

/**
 * @ClassName Credential
 * @Description 一机一密接口
 * @Author lushi
 * @Date 2021/7/18 19:31
 */
@RestController
@RequestMapping("/api/credential")
public class Credential {

    /**
     * 调用GetDeviceCredential查询设备的访问凭证
     *
     * @param ClientId 需要查询访问凭证的设备的Client ID
     * @return DeviceCredential 设备的访问凭证信息 RequestId 公共参数
     * ClientId 设备的Client ID。
     * CreateTime 设备访问凭证创建的毫秒时间戳。
     * DeviceAccessKeyId 设备的AccessKey ID。
     * DeviceAccessKeySecret 设备的AccessKey Secret。
     * InstanceId 微消息队列MQTT版实例的ID。
     * UpdateTime 设备访问凭证上一次更新的毫秒时间戳。
     * @throws Exception 异常
     */
    @ApiOperation(value = "查询设备的访问凭证")
    @RequestMapping(value = "/getcredential",method = {RequestMethod.POST})
    public Result<Object> GetDeviceCredential(@ApiParam(value = "需要查询访问凭证的设备的Client ID",required = true) @RequestParam String ClientId) throws Exception {
        MqttConfigurer mqttConfigurer = new MqttConfigurer();
        com.aliyun.onsmqtt20200420.Client client = MqttClient.createClient(mqttConfigurer.getAccesskey(), mqttConfigurer.getSecretkey(), mqttConfigurer.getEndpoint());
        GetDeviceCredentialRequest getDeviceCredentialRequest = new GetDeviceCredentialRequest()
                .setClientId(ClientId)
                .setInstanceId(mqttConfigurer.getInstanceid());
        try {
            GetDeviceCredentialResponse response = client.getDeviceCredential(getDeviceCredentialRequest);
            JSONObject json = new JSONObject(response.toMap());
            String data = String.valueOf(json.getJSONObject("body"));
            return ResultGenerator.genSuccessResult(data);
        }catch (Exception exception){
            return ResultGenerator.genFailResult(exception.getMessage());
        }
    }

    /**
     * 调用RefreshDeviceCredential更新设备的访问凭证
     *
     * @param ClientId 需要查询访问凭证的设备的Client ID
     * @return DeviceCredential 设备的访问凭证信息 RequestId 公共参数
     * ClientId 设备的Client ID。
     * CreateTime 设备访问凭证创建的毫秒时间戳。
     * DeviceAccessKeyId 设备的AccessKey ID。
     * DeviceAccessKeySecret 设备的AccessKey Secret。
     * InstanceId 微消息队列MQTT版实例的ID。
     * UpdateTime 设备访问凭证上一次更新的毫秒时间戳。
     * @throws Exception 异常
     */
    @ApiOperation(value = "更新设备的访问凭证")
    @RequestMapping(value = "/refreshcredential",method = {RequestMethod.POST})
    public Result<Object> RefreshDeviceCredential(@ApiParam(value = "需要更新访问凭证的设备的Client ID",required = true) @RequestParam String ClientId) throws Exception {
        MqttConfigurer mqttConfigurer = new MqttConfigurer();
        com.aliyun.onsmqtt20200420.Client client = MqttClient.createClient(mqttConfigurer.getAccesskey(), mqttConfigurer.getSecretkey(), mqttConfigurer.getEndpoint());
        RefreshDeviceCredentialRequest refreshDeviceCredentialRequest = new RefreshDeviceCredentialRequest()
                .setClientId(ClientId)
                .setInstanceId(mqttConfigurer.getInstanceid());
        try {
            RefreshDeviceCredentialResponse response = client.refreshDeviceCredential(refreshDeviceCredentialRequest);
            JSONObject json = new JSONObject(response.toMap());
            String data = String.valueOf(json.getJSONObject("body"));
            return ResultGenerator.genSuccessResult(data);
        }catch (Exception exception){
            return ResultGenerator.genFailResult(exception.getMessage());
        }
    }

    /**
     * 调用RegisterDeviceCredential为某个设备注册访问凭证
     *
     * @param ClientId 需要查询访问凭证的设备的Client ID
     * @return DeviceCredentia 设备的访问凭证信息 RequestId 公共参数
     * ClientId 设备的Client ID。
     * CreateTime 设备访问凭证创建的毫秒时间戳。
     * DeviceAccessKeyId 设备的AccessKey ID。
     * DeviceAccessKeySecret 设备的AccessKey Secret。
     * InstanceId 微消息队列MQTT版实例的ID。
     * UpdateTime 设备访问凭证上一次更新的毫秒时间戳。
     * @throws Exception 异常
     */
    @ApiOperation(value = "为某个设备注册访问凭证")
    @RequestMapping(value = "/registercredential",method = {RequestMethod.POST})
    public Result<Object> RegisterDeviceCredential(@ApiParam(value = "需要注册访问凭证的设备的Client ID",required = true)@RequestParam String ClientId) throws Exception {
        MqttConfigurer mqttConfigurer = new MqttConfigurer();
        com.aliyun.onsmqtt20200420.Client client = MqttClient.createClient(mqttConfigurer.getAccesskey(), mqttConfigurer.getSecretkey(), mqttConfigurer.getEndpoint());
        RegisterDeviceCredentialRequest registerDeviceCredentialRequest = new RegisterDeviceCredentialRequest()
                .setClientId(ClientId)
                .setInstanceId(mqttConfigurer.getInstanceid());
        try {
            RegisterDeviceCredentialResponse response = client.registerDeviceCredential(registerDeviceCredentialRequest);
            JSONObject json = new JSONObject(response.toMap());
            String data = String.valueOf(json.getJSONObject("body"));
            return ResultGenerator.genSuccessResult(data);
        }catch (Exception exception){
            return ResultGenerator.genFailResult(exception.getMessage());
        }
    }

    /**
     * 调用UnRegisterDeviceCredential注销设备的访问凭证
     *
     * @param ClientId 需要查询访问凭证的设备的Client ID
     * @return RequestId 公共参数
     * @throws Exception 异常
     */
    @ApiOperation(value = "注销设备的访问凭证")
    @RequestMapping(value = "/unregistercredential",method = {RequestMethod.POST})
    public Result<Object> UnRegisterDeviceCredential(@ApiParam(value = "需要注销访问凭证的设备的Client ID",required = true)@RequestParam String ClientId) throws Exception {
        MqttConfigurer mqttConfigurer = new MqttConfigurer();
        com.aliyun.onsmqtt20200420.Client client = MqttClient.createClient(mqttConfigurer.getAccesskey(), mqttConfigurer.getSecretkey(), mqttConfigurer.getEndpoint());
        UnRegisterDeviceCredentialRequest unRegisterDeviceCredentialRequest = new UnRegisterDeviceCredentialRequest()
                .setClientId(ClientId)
                .setInstanceId(mqttConfigurer.getInstanceid());
        try {
            UnRegisterDeviceCredentialResponse response =  client.unRegisterDeviceCredential(unRegisterDeviceCredentialRequest);
            JSONObject json = new JSONObject(response.toMap());
            String data = String.valueOf(json.getJSONObject("body"));
            return ResultGenerator.genSuccessResult(data);
        }catch (Exception exception){
            return ResultGenerator.genFailResult(exception.getMessage());
        }
    }
}
